//package com.baizhi.baiweiexam.config;
//
//import com.baizhi.baiweiexam.utils.ResponseUtils;
//import com.baizhi.baiweiexam.common.base.Result;
//import com.fasterxml.jackson.core.JsonProcessingException;
//import com.fasterxml.jackson.databind.ObjectMapper;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.autoconfigure.web.ServerProperties;
//import org.springframework.boot.autoconfigure.web.servlet.error.BasicErrorController;
//import org.springframework.boot.autoconfigure.web.servlet.error.ErrorViewResolver;
//import org.springframework.boot.web.error.ErrorAttributeOptions;
//import org.springframework.boot.web.servlet.error.ErrorAttributes;
//import org.springframework.http.HttpStatus;
//import org.springframework.http.MediaType;
//import org.springframework.http.ResponseEntity;
//import org.springframework.web.bind.annotation.RestController;
//import org.springframework.web.servlet.ModelAndView;
//
//import javax.servlet.http.HttpServletRequest;
//import javax.servlet.http.HttpServletResponse;
//import java.util.Collections;
//import java.util.List;
//import java.util.Map;
//
//
///**
// * https://blog.csdn.net/zhongjianboy/article/details/126322783
// */
//@RestController
//public class MyErrorController extends BasicErrorController {
//
//    @Autowired
//    public MyErrorController(ErrorAttributes errorAttributes,
//                             ServerProperties serverProperties,
//                             List<ErrorViewResolver> errorViewResolvers) {
//        super(errorAttributes, serverProperties.getError(), errorViewResolvers);
//    }
//
//
//    @Override
//    public ModelAndView errorHtml(HttpServletRequest request,
//                                  HttpServletResponse response) {
//        HttpStatus status = getStatus(request);
//        Map<String, Object> model = Collections
//                .unmodifiableMap(getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.TEXT_HTML)));
////        return new ModelAndView("error", model, status);
//        Result result = Result.fail(500, (String) model.get("error"));
////        ObjectMapper objectMapper = new ObjectMapper();
////        String resultStr = objectMapper.writeValueAsString(result);
//        ResponseUtils.out(response, result);
//        return null;
//    }
//
//    @Override
//    public ResponseEntity<Map<String, Object>> error(HttpServletRequest request) {
//        Map<String, Object> body = getErrorAttributes(request, getErrorAttributeOptions(request, MediaType.ALL));
//        ErrorAttributeOptions errorAttributeOptions = getErrorAttributeOptions(request, MediaType.ALL);
//
////        //可以换成项目中自定义的通信json
////        Map<String, Object> resultBody = new HashMap<>(16);
////        resultBody.put("success", false);
////        resultBody.put("code", body.get("status"));
////        resultBody.put("message", body.get("error"));
////        Map<String, Object> data = new HashMap<>();
////        data.put("timestamp", body.get("timestamp"));
////        data.put("path", body.get("path"));
////        resultBody.put("data", data);
//        Result result = Result.fail(500, (String) body.get("error"));
//        ObjectMapper objectMapper = new ObjectMapper();
//        Map m = null;
//        try {
//            m = objectMapper.readValue(objectMapper.writeValueAsString(result), Map.class);
//        } catch (JsonProcessingException e) {
//
//        }
//        return new ResponseEntity<>(m, HttpStatus.OK);
//    }
//
//}
